package com.atguigu.gmall.mq.config;

import com.atguigu.gmall.mq.mqconst.MqDeadLetterConst;
import com.atguigu.gmall.mq.mqconst.MqWareConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MqDeadLetterConfig {

    @Bean("directExchangeNormal")
    public DirectExchange directExchangeNormal() {
        return ExchangeBuilder.directExchange(MqDeadLetterConst.EXCHANGE_NORMAL)
                .durable(true).build();
    }

    @Bean("queueNormal")
    public Queue queueNormal() {
        return QueueBuilder.durable(MqDeadLetterConst.QUEUE_NORMAL)
                .deadLetterExchange(MqDeadLetterConst.EXCHANGE_DEAD)
                .deadLetterRoutingKey(MqDeadLetterConst.ROUTING_KEY_DEAD)
                .build();
    }

    @Bean
    public Binding bindingNormal(@Qualifier("queueNormal") Queue queueNormal,
                                 @Qualifier("directExchangeNormal") Exchange directExchangeNormal) {
        return BindingBuilder.bind(queueNormal).to(directExchangeNormal)
                .with(MqDeadLetterConst.ROUTING_KEY_NORMAL).noargs();
    }


    @Bean("exchangeDead")
    public Exchange exchangeDead() {
        return ExchangeBuilder.directExchange(MqDeadLetterConst.EXCHANGE_DEAD)
                .durable(true).build();
    }

    @Bean("queueDead")
    public Queue queueDead() {
        return QueueBuilder.durable(MqDeadLetterConst.QUEUE_DEAD).build();
    }

    @Bean
    public Binding bindingDead(@Qualifier("queueDead") Queue queueDead,
                               @Qualifier("exchangeDead") Exchange exchangeDead) {
        return BindingBuilder.bind(queueDead).to(exchangeDead)
                .with(MqDeadLetterConst.ROUTING_KEY_DEAD).noargs();
    }


    @Bean("getExchangeWare")
    public Exchange getExchangeWare() {
        return ExchangeBuilder.directExchange(MqWareConst.EXCHANGE_WARE).durable(true).build();
    }

    @Bean("getQueueWare")
    public Queue getQueueWare() {
        return QueueBuilder.durable(MqWareConst.QUEUE_WARE).build();
    }

    @Bean
    public Binding getBindingWare(@Qualifier("getExchangeWare") Exchange exchange,
                                  @Qualifier("getQueueWare") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(MqWareConst.ROUTING_KEY_WARE).noargs();
    }










}
